﻿using System.Collections.Generic;
using System.Text;


public static class SQLBuilder
{
    public static string GetInsertSQLByModel<T>(T model)
    {
        var modelType = model.GetType();
        var properties = modelType.GetProperties();

        List<string> keySqlList = new List<string>();
        List<string> valueSqlList = new List<string>();

        foreach (var property in properties)
        {
            var key = property.Name;
            var type = property.PropertyType;
            var value = property.GetValue(model);

            if (!Extension.isNull(value, type) && key != "Id")
            {
                keySqlList.Add($"[{ key }]");
                valueSqlList.Add($"'{value}'");
            }
        }
        string sql = $"\n INSERT INTO [{modelType.Name}] ({string.Join(",", keySqlList)}) VALUES({string.Join(",", valueSqlList)}); \n";

        return sql;
    }

    public static string GetUpdateSQLByModel<T>(T model)
    {
        var modelType = model.GetType();
        var properties = modelType.GetProperties();
        var sqlList = new List<string>();

        foreach (var property in properties)
        {
            var key = property.Name;
            var type = property.PropertyType;
            var value = property.GetValue(model);

            if (!Extension.isNull(value, type) && key != "Id")
            {
                sqlList.Add($"[{key}] = '{value}'");
            }
        }
        string sql = $"UPDATE [{modelType.Name}] SET {string.Join(",", sqlList)}";

        return sql;
    }

    public static string GetIdentity() => "SELECT @@IDENTITY;";


}

